import AntModal from '@/components/pt/dialog/AntModal'
import { getBizCustomer, addBizCustomer, updateBizCustomer, checkCustomerNameUnique } from '@/api/biz/bizCustomer'
import SelectUser from '@/components/pt/selectUser/SelectUser'

export default {
  name: 'CreateForm',
  props: {
    customerCategoryOptions: {
      type: Array,
      required: true
    },

    isClearOptions: {
      type: Array,
      required: true
    },

    sellModeOptions: {
      type: Array,
      required: true
    }

  },
  components: {
    AntModal,

    SelectUser

  },
  data () {
    const validateCustomerName = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error('客户名称不允许为空'))
      } else {
        checkCustomerNameUnique(this.form.id, this.form.customerName)
        callback()
      }
    }
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加客户档案',
      // 表单参数
      form: {},
      rules: {
        customerCategory: [{ required: true, message: '客户类别不能为空', trigger: 'blur' }],

        customerName: [{ required: true, message: '客户名称不能为空', validator: validateCustomerName, trigger: 'blur' }],

        mnemonicCode: [{ required: true, message: '助记码不能为空', trigger: 'blur' }],

        isClear: [{ required: true, message: '清账发货不能为空', trigger: 'blur' }],

        phone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],

        contactPerson: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],

        sellMode: [{ required: true, message: '售价模式不能为空', trigger: 'blur' }],

        customernan: [{ required: true, message: '业务员不能为空', trigger: 'blur' }]

      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      console.log('this.isClearOptions :>> ', this.isClearOptions);
      this.form = {
        id: undefined,
        customerCategory: undefined,

        customerCode: undefined,

        customerName: undefined,

        mnemonicCode: undefined,

        isClear: 'N',
        inventoryFlag: 'Y',

        phone: undefined,

        contactPerson: undefined,

        sellMode: undefined,

        customernan: undefined,

        remark: undefined

      }
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.formTitle = '添加客户档案'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const bizCustomerId = row.id
      getBizCustomer(bizCustomerId).then(response => {
        response.data.customernan = { ids: response.data.customernan, names: response.data.customernanName }
        this.form = response.data
        this.formTitle = '修改客户档案'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.customernan !== undefined) {
            saveForm.customernan = this.form.customernan.ids
          }
          if (this.form.id !== undefined) {
            updateBizCustomer(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addBizCustomer(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizcustomer/index'
      this.$router.push(index)
    }

  }
}
